function getCarts() {
    // 查询购物车,显示到minicart
    $.ajax({
        url: HOST + "webapi/cart/getgood",
        type: "get",
        headers:{"token":localStorage.getItem("token")},
        data: {userId:localStorage.getItem("userId")},
        success: function (res) {
            cartList = res.data;
            var s= '';
            var sum = 0;    // 计算总价格
            if(res.data!=null && res.data.length>0) {
                $(".quantity:eq(0)").html(res.data.length);   // 显示购物车商品数量
                for (let i = 0; i < res.data.length; i++) {
                    s += `<li class="minicart-product">
                            <a class="product-item_remove" href="#" onclick="delCart(${i})"><i
                                    class="pe-7s-close"></i></a>
                            <a href="single-product.html?gid="+${res.data[i].goods.id} class="product-item_img">
                                <img class="img-full" src="${res.data[i].goods.picture}"
                                     alt="Product Image">
                            </a>
                            <div class="product-item_content">
                                <a class="product-item_title" href="single-product.html?gid="+${res.data[i].goods.id}>Black Pepper Grains</a>
                                <span class="product-item_quantity">${res.data[i].num} x $${res.data[i].money}</span>
                            </div>
                        </li>`;
                    sum+=res.data[i].num*res.data[i].money;
                }
                $(".ammount").html("$"+sum);   // 显示总价格
                $(".minicart-list").html('');
                $(".minicart-list").html(s);
            }else{
                $(".quantity").hide();
                let blank = `<div class="group-btn_wrap d-grid gap-2">
                    <a href="#" class="btn btn-secondary btn-primary-hover">Empty Cart</a>
                </div>`;
                $("#miniCart .offcanvas-body").html(blank);
            }
        }
    })
}
getCarts();

var cartList = [];
function delCart(i) {
    layer.confirm(i18np.prop('confirm.delete'), {
        title: i18np.prop('btn.tishi'),
        btn: [i18np.prop('btn.ok'),i18np.prop('btn.cancel')]}, function(index){
        $.ajax({
            url:HOST+"webapi/cart/removeGoods"
            ,type:"get"
            ,data:{
                cartid:cartList[i].id,
                userId:localStorage.getItem("userId")
            }
            ,contentType: 'application/json'
            ,headers:{"token":localStorage.getItem("token")}
            ,success:function (res) {
                if(res.code==200){
                    layer.msg("删除成功！",{icon:1});
                    setTimeout(function(){
                        var index = layer.getFrameIndex(window.name);
                        layer.close(index);//关闭弹出层
                        getCarts();
                        // location.reload();//重新加载父页面
                    },1500);
                }else{
                    layer.msg("删除失败！",{icon:2});
                }
            }
            ,error:function () {
                console.log("ajax error"),{icon:2};
            }
        });
        layer.close(index);
    },function(index){
        layer.close(index);
    })
}